/**
 * 
 */
package com.jonosoft.photocast.viewer.web.client.ui.imagecollections;

import java.util.HashMap;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.Widget;
import com.jonosoft.photocast.viewer.web.client.ImageCollection;

/**
 * TODO Add description ImageCollectionsMenuItem (com.jonosoft.photocast.viewer.web.client.ui.imagecollections.ImageCollectionsMenuItem)
 *
 * @author jon
 *
 */
public class ImageCollectionsMenuItem extends Composite implements SourcesClickEvents, MouseListener {

	public static String STYLE_NAME = "image-collections-menu-item";
	private static String STYLE_NAME_OVER = STYLE_NAME + "-over";
	private static String STYLE_NAME_ACTIVE = STYLE_NAME + "-active";

	public static interface CommandDelegate {
		public void onExecute(ImageCollectionsMenuItem menuItem);
	}

	private final Label label = new Label();
	private final HashMap routedClickListeners = new HashMap();

	protected ImageCollection imageCollection = null;

	protected ImageCollectionsMenuItem() {
		initWidget(label);

		label.addMouseListener(this);

		setStyleName(STYLE_NAME);
	}

	public ImageCollectionsMenuItem(ImageCollection imageCollection) {
		this();
		this.imageCollection = imageCollection;
	}

	public ImageCollection getImageCollection() {
		return this.imageCollection;
	}

	public void setImageCollection(ImageCollection imageCollection) {
		this.imageCollection = imageCollection;
	}
	
	public String getText() {
		return this.label.getText();
	}
	
	public void setText(String text) {
		this.label.setText(text);
	}

	/**
	 * @see com.google.gwt.user.client.ui.MouseListener#onMouseDown(com.google.gwt.user.client.ui.Widget, int, int)
	 */
	public void onMouseDown(Widget sender, int x, int y) {
		addStyleName(STYLE_NAME_ACTIVE);
	}

	/**
	 * @see com.google.gwt.user.client.ui.MouseListener#onMouseUp(com.google.gwt.user.client.ui.Widget, int, int)
	 */
	public void onMouseUp(Widget sender, int x, int y) {
		removeStyleName(STYLE_NAME_ACTIVE);
	}

	/**
	 * @see com.google.gwt.user.client.ui.MouseListener#onMouseEnter(com.google.gwt.user.client.ui.Widget)
	 */
	public void onMouseEnter(Widget sender) {
		addStyleName(STYLE_NAME_OVER);
	}

	/**
	 * @see com.google.gwt.user.client.ui.MouseListener#onMouseLeave(com.google.gwt.user.client.ui.Widget)
	 */
	public void onMouseLeave(Widget sender) {
		removeStyleName(STYLE_NAME_OVER);
	}

	/**
	 * @see com.google.gwt.user.client.ui.MouseListener#onMouseMove(com.google.gwt.user.client.ui.Widget, int, int)
	 */
	public void onMouseMove(Widget sender, int x, int y) {
	}

	/**
	 * @see com.google.gwt.user.client.ui.SourcesClickEvents#addClickListener(com.google.gwt.user.client.ui.ClickListener)
	 */
	public void addClickListener(final ClickListener listener) {
		final RoutedClickListener routedListener = new RoutedClickListener(listener, this);
		routedClickListeners.put(listener, routedListener);
		label.addClickListener(routedListener);
	}

	/**
	 * @see com.google.gwt.user.client.ui.SourcesClickEvents#removeClickListener(com.google.gwt.user.client.ui.ClickListener)
	 */
	public void removeClickListener(final ClickListener listener) {
		final RoutedClickListener routedListener = (RoutedClickListener) routedClickListeners.get(listener);
		label.removeClickListener(routedListener);
		routedClickListeners.remove(listener);
	}

	/**
	 * TODO Add description RoutedClickListener (com.jonosoft.photocast.viewer.web.client.ui.imagecollections.RoutedClickListener)
	 *
	 * @author jon
	 */
	private class RoutedClickListener implements ClickListener {

		private ClickListener destinationClickListener = null;
		private ImageCollectionsMenuItem senderObject = null;

		public RoutedClickListener(final ClickListener destinationClickListener, final ImageCollectionsMenuItem senderObject) {
			this.destinationClickListener = destinationClickListener;
			this.senderObject = senderObject;
		}

		/**
		 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
		 */
		public void onClick(final Widget sender) {
			destinationClickListener.onClick(senderObject);
		}

	}

}
